package edu.gyc.stu.dao;

import edu.gyc.stu.model.Student;
import edu.gyc.stu.util.ConnectPool;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

public class StuDao {
    private static Logger logger = Logger.getLogger(StuDao.class.getName());

    private ConnectPool pool;

    public StuDao(ConnectPool pool) {
        this.pool = pool;
    }

    public List<Student> getAllStu() {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        List<Student> stus = new ArrayList<>();
        try {
            conn = pool.getConnection();
            String sql = "select * from tb_stu order by id";
            stmt = conn.prepareStatement(sql);
            rs = stmt.executeQuery();
            while (rs.next()) {
                Student student = new Student();
                student.setId(rs.getInt("Id"));
                student.setName(rs.getString("name"));
                student.setGender(rs.getString("gender"));
                student.setBirthday(rs.getDate("birthday"));
                student.setMajor(rs.getString("major"));
                stus.add(student);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            pool.releaseConnction(conn);
        }
        return stus;
    }

    public List<Student> getStuPage(int page, int pageSize) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        List<Student> stus = new ArrayList<>();

        int start = 0;
        if (page > 0) {
            start = (page - 1) * pageSize;
        }

        try {
            conn = pool.getConnection();
            String sql = "select * from tb_stu order by id desc limit ?,?";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, start);
            stmt.setInt(2, pageSize);
            rs = stmt.executeQuery();
            while (rs.next()) {
                Student student = new Student();
                student.setId(rs.getInt("Id"));
                student.setName(rs.getString("name"));
                student.setGender(rs.getString("gender"));
                student.setBirthday(rs.getDate("birthday"));
                student.setMajor(rs.getString("major"));
                stus.add(student);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            pool.releaseConnction(conn);

        }
        return stus;
    }

    public int getTotal() {
        Connection connection = null;
        try {
            connection = pool.getConnection();
            PreparedStatement statement = connection.prepareStatement("select count(*) as total from tb_stu ");
            ResultSet resultSet = statement.executeQuery();
            resultSet.next();
            return resultSet.getInt("total");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            pool.releaseConnction(connection);
        }
        return 0;
    }

    public int getTotalPages(int pageSize) {
        int total = getTotal();
        int totalPages = (total % pageSize == 0) ? total / pageSize : total / pageSize + 1;
        return totalPages;
    }

    public boolean addStu(Student student) {
        Connection connection = pool.getConnection();
        String sql = "insert into tb_stu (name,gender,birthday,major) values(?,?,?,?)";
        int r = 0;
        logger.info(student.toString());
        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, student.getName());
            statement.setString(2, student.getGender());
            statement.setDate(3, (Date) student.getBirthday());
            statement.setString(4, student.getMajor());

            r = statement.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            pool.releaseConnction(connection);
        }
        if (r == 1) {
            return true;
        }
        return false;
    }

    public boolean delStu(int id) {
        Connection connection = pool.getConnection();
        String sql = "delete from tb_stu where id=?";
        int r = 0;

        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setInt(1, id);
            r = statement.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            pool.releaseConnction(connection);
        }
        if (r == 1) {
            return true;
        }
        return false;
    }

    public boolean updateStu(Student student) {
        Connection connection = pool.getConnection();
        String sql = "update tb_stu set name=? ,gender=?,birthday=?,major=? where id=?";
        int r = 0;
        logger.info( student.toString());
        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, student.getName());
            statement.setString(2, student.getGender());
            statement.setDate(3, (Date) student.getBirthday());
            statement.setString(4, student.getMajor());
            statement.setInt(5, student.getId());
            r = statement.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            pool.releaseConnction(connection);
        }
        if (r == 1) {
            return true;
        }
        return false;
    }
}
